﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace HML.Design
{
    /// <summary>
    /// 透明颜色预览效果控件
    /// </summary>
    [ToolboxItem(false)]
    public class AlphaColorEditorPreviewControl : Control
    {
        #region 属性

        private Color alphaColor = Color.Empty;
        /// <summary>
        /// 颜色
        /// </summary>
        public Color AlphaColor
        {
            get { return this.alphaColor; }
            set
            {
                if (this.alphaColor == value)
                    return;

                this.alphaColor = value;
                this.Invalidate();
            }
        }

        #endregion

        public AlphaColorEditorPreviewControl()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.StandardClick, false);
            SetStyle(ControlStyles.StandardDoubleClick, false);
            SetStyle(ControlStyles.Selectable, false);
        }

        #region 重写

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (
                (this.AlphaColor.A == 0 && this.AlphaColor.R == 255 && this.AlphaColor.G == 255 && this.AlphaColor.B == 255) ||
                (this.AlphaColor.A == 0 && this.AlphaColor.R == 0 && this.AlphaColor.G == 0 && this.AlphaColor.B == 0)
                )
                return;

            Graphics g = e.Graphics;
            SolidBrush color_sb = new SolidBrush(Color.White);

            g.FillRectangle(color_sb, this.ClientRectangle);

            color_sb.Color = Color.FromArgb(byte.MaxValue, this.AlphaColor);
            Region color_r = new Region(e.ClipRectangle);
            GraphicsPath color_gp = new GraphicsPath();
            color_gp.AddEllipse(new Rectangle(e.ClipRectangle.X + 3, e.ClipRectangle.Y + 3, e.ClipRectangle.Width - 6, e.ClipRectangle.Height - 6));
            color_r.Exclude(color_gp);
            g.FillRegion(color_sb, color_r);
            color_r.Dispose();
            color_gp.Dispose();

            string str = "透\r\n明\r\n效\r\n果";
            color_sb.Color = Color.Black;
            Size str_size = Size.Ceiling(g.MeasureString(str, this.Font, int.MaxValue, StringFormat.GenericTypographic));
            g.DrawString(str, this.Font, color_sb, new Point((this.ClientRectangle.Width - str_size.Width) / 2, (this.ClientRectangle.Height - str_size.Height) / 2), StringFormat.GenericTypographic);

            color_sb.Color = this.AlphaColor;
            g.FillEllipse(color_sb, new Rectangle(e.ClipRectangle.X + 3, e.ClipRectangle.Y + 3, e.ClipRectangle.Width - 6, e.ClipRectangle.Height - 6));

            Pen border_pen = new Pen(Color.FromArgb(224, 224, 224));
            g.DrawRectangle(border_pen, new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1));
            border_pen.Dispose();

            color_sb.Dispose();
        }

        #endregion
    }


}
